package com.localjoycommunity.cpdj.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.localjoycommunity.cpdj.project.system.domain.SysLearnData;
import com.localjoycommunity.cpdj.project.system.domain.User;
import com.localjoycommunity.cpdj.project.system.domain.UserData;
import com.localjoycommunity.cpdj.project.system.domain.UserNews;
import com.localjoycommunity.cpdj.project.system.mapper.UserDataMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserMapper;
import com.localjoycommunity.cpdj.project.system.service.ISysLearnDataService;
import com.localjoycommunity.cpdj.project.system.service.IUserDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lida
 * @Date: 2020/11/14
 * @Description:
 */
@Service
public class UserDataServiceImpl extends ServiceImpl<UserDataMapper, UserData> implements IUserDataService {

    @Autowired
    private ISysLearnDataService sysLearnDataService;
    @Autowired
    private UserMapper userMapper;
    /**
     * 用户收藏学习资料
     *
     * @param userData
     */
    @Override
    public int dataDig(UserData userData) {
        // 1. 先查询
        UserData one = baseMapper.selectOne(new QueryWrapper<UserData>().lambda().eq(UserData::getUserId, userData.getUserId())
                .eq(UserData::getLearnId, userData.getLearnId()));
        if (one == null) {
            userData.setIsDig(1);
            sysLearnDataService.updateDigNum(userData.getLearnId(), 1);
            return baseMapper.insert(userData);
        }
        User user = userMapper.selectUserById(userData.getUserId());
        Long collectNum = user.getCollectNum() == null ? 0 : user.getCollectNum();
        one.setIsDig(one.getIsDig() ^ 1);
        if (one.getIsDig() == 1) {
            sysLearnDataService.updateDigNum(userData.getLearnId(), 1);
            user.setCollectNum(collectNum + 1);
        } else {
            sysLearnDataService.updateDigNum(userData.getLearnId(), -1);
            collectNum = collectNum - 1 <= 0 ? 0 : collectNum - 1;
            user.setCollectNum(collectNum);
        }
        userMapper.updateUser(user);
        return baseMapper.updateById(one);
    }

    /**
     * 收藏学习资料列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysLearnData> selectUserData(Integer userId) {
        List<UserData> userDatas = baseMapper.selectList(new QueryWrapper<UserData>().lambda().eq(UserData::getUserId, userId).eq(UserData::getIsDig, 1));
        if (userDatas == null || userDatas.size() == 0) {
            return null;
        }
        List<SysLearnData> list = new ArrayList<>();
        for (UserData userData : userDatas) {
            SysLearnData sysLearnData = sysLearnDataService.selectSysLearnDataById(userData.getLearnId());
            list.add(sysLearnData);
        }
        return list;
    }

    @Override
    public Integer isDig(int userId, Long learnId) {
        return baseMapper.isDig(userId, learnId).orElse(0);
    }
}
